package com.njust.service.Impl;


import com.njust.BO.TempTendencyBo;
import com.njust.common.Const;
import com.njust.dataobject.*;
import com.njust.form.TempTendencyForm;
import com.njust.mapper.*;

import com.njust.properities.QixiProperties;
import com.njust.service.GearService;
import com.njust.service.MotorService;
import com.njust.utils.BigDecimalUtil;
import com.njust.utils.CompareUtil;
import com.njust.utils.DateUtil;
import com.njust.utils.StringUtil;
import com.njust.vo.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.text.ParseException;
import java.util.*;
import java.util.stream.Collectors;

/**
 * Created by zhujiawei on 2017/12/28.
 */
@Service
@Slf4j
public class MotorServiceImpl implements MotorService {

    @Autowired
    private TrainLaserMapper trainLaserMapper;

    @Autowired
    private MotorInfoMapper motorInfoMapper;

    @Autowired
    private TrainInfoMapper trainInfoMapper;

    @Autowired
    private GearInfoMapper gearInfoMapper;
    @Autowired
    private GearService gearService;
    @Autowired
    private TrainParamMapper trainParamMapper;

    @Autowired
    private QixiProperties qixiProperties;

    /**
     * 查询dao层 返回左右激光原始数据
     * @param trainOnlyid
     * @param motorNum
     * @return
     */
    @Override
    public ResultVO findByTrainOnlyidAndMotorNum(Long trainOnlyid,Integer motorNum,Integer trainDirection) {
        LaserVO laserVO=new LaserVO();
        Map<String,List<Double>> map=new HashMap<>();
        List<TrainLaser> trainLaserDataList = trainLaserMapper.findByTrainIdAndMotorNum(trainOnlyid, motorNum);
        if(trainLaserDataList.size()==0){
            return ResultVO.createByErrorMessage("无数据");
        }
        //数据的封装 左右激光数据加入
        List<Double> leftLaserData=trainLaserDataList.stream().map(e->
                new Double(e.getLeftLaser())
        ).collect(Collectors.toList());

        List<Double> rightLaserData=trainLaserDataList.stream().map(e->
                new Double(e.getRightLaser())
        ).collect(Collectors.toList());

        TrainParam trainParam = trainParamMapper.findByUdFlag(trainDirection);
        if(trainParam.getGapWarn()!=null){
            laserVO.setGapWarn(trainParam.getGapWarn());
        }
        //封装到ResultVO
        map.put("left",leftLaserData);
        map.put("right",rightLaserData);
        laserVO.setMap(map);
        laserVO.setLowerLimit(qixiProperties.getLaser().getLowerLimit());
        laserVO.setUpperLimit(qixiProperties.getLaser().getUpperLimit());
        return ResultVO.createBySuccess(laserVO);
    }


    /**
     * 根据车号和行驶方向 返回电机的详细报表
     * @param trainOnlyid
     * @return
     */
    @Override
    public ResultVO findByTrainOnlyid(Long trainOnlyid,Integer trainDirection){
        MotorDataVO motorDataVO=new MotorDataVO();
        List<MotorVO> motorVOList=new ArrayList<>();

        TrainParam trainParam= trainParamMapper.findByUdFlag(trainDirection);


        //筛选出8条记录 电机
        List<MotorInfo> motorInfoList = motorInfoMapper.findByTrainIdOrderByMotorNum(trainOnlyid);
        if(motorInfoList.size()==0){
            return ResultVO.createByErrorMessage("无数据");
        }
        //查询主表
        TrainInfo trainInfo = trainInfoMapper.selectByPrimaryKey(trainOnlyid);
        if(trainInfo==null){
            return ResultVO.createByErrorMessage("无数据");
        }
        //基本信息进行复制
        BeanUtils.copyProperties(trainInfo,motorDataVO);
        //处理八个电机的具体数据
        for(MotorInfo motorInfo:motorInfoList){
            MotorVO motorVO=new MotorVO();
            //具体齿的数据（左/右）
            if(motorInfo.getMotorId()==null){
                return ResultVO.createByErrorMessage("无数据");
            }else {
                //添加排障器的值
                GearInfo LgapInfo = gearInfoMapper.findByMotorIdAndGearNum(motorInfo.getMotorId(), motorInfo.getLgapMin());
                if(LgapInfo==null || LgapInfo.getLgapValue()==null){
                    motorVO.setLgapMin("无数据");
                }else {
                    motorVO.setLgapMin(StringUtil.changeChi(LgapInfo.getLgapValue(),motorInfo.getLgapMin()));
                    if(trainParam!=null){
                        //如果返回的是1 那么就预警 2为报警
                        Integer result = CompareUtil.compare(Const.GEAR, trainParam.getGapWarn()
                                , trainParam.getGapAlarm(), LgapInfo.getLgapValue());
                        motorVO.setLgapStatus(result);
                    }
                }

                GearInfo RgapInfo = gearInfoMapper.findByMotorIdAndGearNum(motorInfo.getMotorId(),motorInfo.getRgapMin());
                if(RgapInfo==null || RgapInfo.getRgapValue()==null){
                    motorVO.setRgapMin("无数据");
                }else {
                    motorVO.setRgapMin(StringUtil.changeChi(RgapInfo.getRgapValue(),motorInfo.getRgapMin()));
                    if(trainParam!=null){
                        //如果返回的是1 那么就预警 2为报警
                        Integer result = CompareUtil.compare(Const.GEAR, trainParam.getGapWarn()
                                , trainParam.getGapAlarm(), RgapInfo.getRgapValue());
                        motorVO.setRgapStatus(result);
                    }
                }
                //槽楔数据
                GearInfo LslothMaxInfo = gearInfoMapper.findByMotorIdAndGearNum(motorInfo.getMotorId(),motorInfo
                        .getLslothMax());
                if(LslothMaxInfo==null || LslothMaxInfo.getLslotValue()==null){
                    motorVO.setLslothMax("无数据");
                }else {
                    motorVO.setLslothMax(StringUtil.changeCao(LslothMaxInfo.getLslotValue(),motorInfo.getLslothMax()));
                    if(trainParam!=null){
                        //如果返回的是1 那么就预警 2为报警
                        Integer result = CompareUtil.compare(Const.SLOT, trainParam.getSlotWarn()
                                , trainParam.getSlotAlarm(), LslothMaxInfo.getLslotValue());
                        motorVO.setLslothMaxStatus(result);
                    }
                }

                GearInfo RslothMaxInfo = gearInfoMapper.findByMotorIdAndGearNum(motorInfo.getMotorId(),motorInfo
                        .getRslothMax());
                if(RslothMaxInfo==null || RslothMaxInfo.getRslotValue()==null){
                    motorVO.setRslothMax("无数据");
                }else {
                    motorVO.setRslothMax(StringUtil.changeCao(RslothMaxInfo.getRslotValue(),motorInfo.getRslothMax()));
                    if(trainParam!=null){
                        //如果返回的是1 那么就预警 2为报警
                        Integer result = CompareUtil.compare(Const.SLOT, trainParam.getSlotWarn()
                                , trainParam.getSlotAlarm(), RslothMaxInfo.getRslotValue());
                        motorVO.setRslothMaxStatus(result);
                    }
                }

                //槽深数据 （左/右）(这里面到底存的是什么值,我知道了，一个村的是槽编号一个存的是值)
                GearInfo LSlotInfo=gearInfoMapper.findByMotorIdAndGearNum(motorInfo.getMotorId(),motorInfo.getLslotMin());
                if(LSlotInfo==null || LSlotInfo.getLslotDepth()==null){
                   motorVO.setLslotMin("无数据");
                }else {
                    motorVO.setLslotMin(StringUtil.changeCao(LSlotInfo.getLslotDepth(),motorInfo.getLslotMin()));
                    if(trainParam!=null){
                        //如果返回的是1 那么就预警 2为报警
                        Integer result = CompareUtil.compare(Const.SLOTDEPTH, trainParam.getSlotdWarn()
                                , trainParam.getSlotdAlarm(), LSlotInfo.getLslotDepth());
                        //if(result==0){
                           // result = CompareUtil.compare(Const.SLOTUDEPTH,trainParam.getSlotuWarm(),trainParam.getSlotuAlarm(),LSlotInfo.getLslotDepth());
                        //}
                        motorVO.setLslotMinStatus(result);
                    }
                }

                GearInfo RSlotInfo=gearInfoMapper.findByMotorIdAndGearNum(motorInfo.getMotorId(),motorInfo.getRslotMin());
                if(RSlotInfo==null || RSlotInfo.getRslotDepth()==null){
                    motorVO.setRslotMin("无数据");
                }else {
                    motorVO.setRslotMin(StringUtil.changeCao(RSlotInfo.getRslotDepth(), motorInfo.getRslotMin()));
                    if (trainParam != null) {
                        //如果返回的是1 那么就预警 2为报警
                        Integer result = CompareUtil.compare(Const.SLOTDEPTH, trainParam.getSlotdWarn()
                                , trainParam.getSlotdAlarm(), RSlotInfo.getRslotDepth());

                        motorVO.setRslotMinStatus(result);
                    }
                }

                    //新添加槽深的上限预警报警状态
                    //右
                    GearInfo RGearInfo = gearInfoMapper.findByMotorIdAndGearNum(motorInfo.getMotorId(),motorInfo.getRslotMax());
                    if(RGearInfo==null ||RGearInfo.getRslotDepth()==null){
                        motorVO.setRslotMax("无数据");
                    }else {
                        motorVO.setRslotMax(StringUtil.changeCao(RGearInfo.getRslotDepth(), motorInfo.getRslotMax()));
                        if (trainParam != null) {
                            //如果返回的是1 那么就预警 2为报警
                            Integer  result = CompareUtil.compare(Const.SLOTUDEPTH,trainParam.getSlotuWarm(),trainParam.getSlotuAlarm(),RGearInfo.getRslotDepth());
                            motorVO.setRslotMaxStatus(result);
                        }
                    }
                    //左
                GearInfo LGearInfo = gearInfoMapper.findByMotorIdAndGearNum(motorInfo.getMotorId(),motorInfo.getLslotMax());
                if(LGearInfo==null ||LGearInfo.getLslotDepth()==null){
                    motorVO.setLslotMax("无数据");
                }else {
                    motorVO.setLslotMax(StringUtil.changeCao(LGearInfo.getLslotDepth(), motorInfo.getLslotMax()));
                    if (trainParam != null) {
                        //如果返回的是1 那么就预警 2为报警
                        Integer  result = CompareUtil.compare(Const.SLOTUDEPTH,trainParam.getSlotuWarm(),trainParam.getSlotuAlarm(),LGearInfo.getLslotDepth());
                        motorVO.setLslotMaxStatus(result);
                    }
                }

                /**
                 * 这里是对温度的处理,需要将上行温度的值改为从上行数据中进行读取
                  */
               // if(motorInfo.getTempAverage()!=null){
                   // motorVO.setTempAverage(motorInfo.getTempAverage());
               // }
                if(motorInfo.getTempMax()!=null){
                    motorVO.setTempMax(motorInfo.getTempMax());
                    if(trainParam!=null){
                        //如果返回的是1 那么就预警 2为报警
                        Integer result = CompareUtil.compare(Const.TEMP, trainParam.getTempWarn()
                                , trainParam.getTempWarn(), motorInfo.getTempMax());
                        motorVO.setTempMaxStatus(result);
                    }
                }
                //设置电机的速度
                motorVO.setMotorSpeed(BigDecimalUtil.dUtil(motorInfo.getMotorSpeed(),2));
                //设置排障器左右高度值

                if(motorInfo.getLpilotValue()!=null){
                    motorVO.setLpilotValue(motorInfo.getLpilotValue());
                    if(trainParam!=null){
                        Integer result = CompareUtil.compare(Const.PILOT,trainParam.getPilotWarm(),trainParam.getPilotAlarm(),motorVO.getLpilotValue());
                        motorVO.setLpilotStatus(result);
                    }
                }
                 //右
                if(motorInfo.getRpilotValue()!=null){
                    motorVO.setRpilotValue(motorInfo.getRpilotValue());
                    if(trainParam!=null){
                        Integer result = CompareUtil.compare(Const.PILOT,trainParam.getPilotWarm(),trainParam.getPilotAlarm(),motorVO.getRpilotValue());
                        motorVO.setRpilotStatus(result);
                    }
                }
                if (motorInfo.getMotorNum()!=null){
                    Map<String,Double> byTrainOnlyidMaxDifference = gearService.findByTrainOnlyidMaxDifference(trainOnlyid, motorInfo.getMotorNum(), trainDirection);
                    motorVO.setMaxLeftDifference(byTrainOnlyidMaxDifference.get("leftMax"));
                    motorVO.setMaxRightDifference(byTrainOnlyidMaxDifference.get("rightMax"));
                }
                //异常处理
                if(motorInfo.getExcValue() !=null){
                    motorVO.setExcValue(motorInfo.getExcValue());
                    if(trainParam!=null){
                        Integer result = CompareUtil.compare(Const.EXC, trainParam.getExpWarn()
                                , trainParam.getExpAlarm(), motorInfo.getExcValue());
                        motorVO.setExcStatus(result);
                    }
                }
                motorVOList.add(motorVO);
            }
        }
        System.out.println(motorVOList);
        motorDataVO.setMotorVOList(motorVOList);
        return ResultVO.createBySuccess(motorDataVO);
    }

    /**
     * 新添加的温度曲线的显示
     */
    @Override
   public  ResultVO findTempTendency(TempTendencyForm tempTendencyForm) throws ParseException {
        //进行相应业务的书写
        //首先根据上下行以及车号,时间进行列车唯一编号的获取
            Date startDate = DateUtil.String2Date(tempTendencyForm.getStartTime());
            Date  endDate = DateUtil.String2Date(tempTendencyForm.getEndTime());
            List<TrainInfo> trainInfos = trainInfoMapper.findByTrainDateAndDirection(startDate, endDate, tempTendencyForm.getTrainId(), tempTendencyForm.getTrainDirection());
         //获取列车唯一编号
        if(trainInfos.size()==0||trainInfos==null){
            //表明没有相关数据
            return ResultVO.createByErrorMessage("无数据");
        }
        List<Long> trainOnlyids = trainInfos.stream()
                .map(e -> e.getTrainOnlyid()).collect(Collectors.toList());
      //根据trainOnlyId和电机号进行温度最大值得查询
       MotorInfoExample motorInfoExample = new MotorInfoExample();
        MotorInfoExample.Criteria criteria = motorInfoExample.createCriteria();
        criteria.andTrainOnlyidIn(trainOnlyids);
        criteria.andMotorNumEqualTo(tempTendencyForm.getMotorNum());
        motorInfoExample.setOrderByClause("motor_date ASC");
        List<MotorInfo> motorInfos = motorInfoMapper.selectByExample(motorInfoExample);
            if(motorInfos.size()==0||motorInfos==null){
                return ResultVO.createByErrorMessage("无数据");
            }
        //进行数据的封装
        TempTendencyVO tempTendencyVO = new TempTendencyVO();
       List<TempTendencyBo> tempTendencyBos = new ArrayList<>();
       tempTendencyVO.setTrainId(tempTendencyForm.getTrainId());
       tempTendencyVO.setMotroNum(tempTendencyForm.getMotorNum());
       for(MotorInfo motorInfo:motorInfos){
           //进行单个数据的封装
         TempTendencyBo tempTendencyBo = new TempTendencyBo();
         BeanUtils.copyProperties(motorInfo,tempTendencyBo);
         tempTendencyBo.setMotorDate(DateUtil.Date2String(motorInfo.getMotorDate()));
         tempTendencyBos.add(tempTendencyBo);
       }
       tempTendencyVO.setTempTendencyBos(tempTendencyBos);
        return ResultVO.createBySuccess(tempTendencyVO);
    }







}
